import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
# 状态位
# 地块去年种植的作物编号
k1_shucai_1 = [0,
               20, 28, 21, 22, 17, 18, 16, 16,
               18, 24, 25, 26, 28, 27, 19, 19, 18, 17, 17, 22, 21, 29, [30, 27], 31,
               [32, 33], [25, 26], 17, 19]
k1_liangshi = [0, 6, 7, 7, 1, 4, 8, 6, 2, 3, 4, 5, 8, 6, 8, 9, 10, 1, 7, 14, 15, 11, 12, 1, 13, 6, 3]
# 地块去年是否种植豆类
k_def = [0,
         4, 4, 4, 4, 5, 5, 4, 4, 5, 4,
         4, 4, 4, 4, 5, 5, 5, 5, 5, 4,
         4, 4, 4, 4, 4, 4, 5, 5]
k_abc = [0,
         2, 2, 2, 3, 3, 2, 2, 3, 3, 3,
         3, 2, 2, 2, 2, 2, 3, 2, 2, 2,
         2, 2, 3, 2, 2, 3]
files = [
    ' ',
    'A1', 'A2', 'A3', 'A4', 'A5', 'A6',
    'B1', 'B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'B8', 'B9', 'B10', 'B11', 'B12', 'B13', 'B14',
    'C1', 'C2', 'C3', 'C4', 'C5', 'C6',
    'D1', 'D2', 'D3', 'D4', 'D5', 'D6', 'D7', 'D8',
    'E1', 'E2', 'E3', 'E4', 'E5', 'E6', 'E7', 'E8', 'E9', 'E10', 'E11', 'E12', 'E13', 'E14', 'E15', 'E16',
    'F1', 'F2', 'F3', 'F4'
]
danjichan = [  #
    [0, 400, 500, 400, 350, 415, 800, 1000, 400, 630, 525, 110, 3000, 2200, 420, 525, 500,
     3000, 2000, 3000, 2000, 2400,
     6400, 2700, 2400, 3300, 3700, 4100, 3200, 12000, 4100, 1600, 10000, 5000, 5500],
    [0, 380, 475, 380, 330, 395, 760, 950, 380, 600, 500, 105, 2850, 2100, 400, 500, 500, 3600, 2400, 3600, 2400, 3000,
     8000, 3300, 3000, 4000, 4500, 5000, 4000, 15000, 5000, 2000, 12000, 6000, 6600],
    [0, 360, 450, 360, 315, 375, 720, 900, 360, 570, 475, 100, 2700, 2000, 380, 475, 500, 3600, 2400, 3600, 2400, 3000,
     8000, 3300, 3000, 4000, 4500, 5000, 4000, 15000, 5000, 2000, 12000, 6000, 6600]
]
twojichan = [  # 17-34
    0, 3200, 2200, 3200, 2200, 2700, 7200, 3000, 2700, 3600, 4100, 4500, 3600, 13500, 4500, 1800, 11000, 5400, 6000,
    5000, 4000, 3000,
    5000, 4000, 10000, 1000
]
foods = [
    " ",
    "黄豆", "黑豆", "红豆", "绿豆", "爬豆",
    "小麦", "玉米", "谷子", "高粱", "黍子",
    "荞麦", "南瓜", "红薯", "莜麦", "大麦",
    "水稻", "豇豆", "刀豆", "芸豆", "土豆",
    "西红柿", "茄子", "菠菜", "青椒", "菜花",
    "包菜", "油麦菜", "小青菜", "黄瓜", "生菜",
    "辣椒", "空心菜", "黄心菜", "芹菜", "大白菜",
    "白萝卜", "红萝卜", "榆黄菇", "香菇", "白灵菇", "羊肚菌"
]
danji_muben = [0, 400, 400, 350, 350, 350, 450, 500, 360, 400, 360, 350, 1000, 2000, 400, 350, 680]
danji_danjia = [0, 3.25, 7.5, 8.25, 7, 6.75, 3.5, 3, 6.75, 6, 7.5, 40, 1.5, 3.25, 5.5, 3.5, 7]
yiji_muben = [
    [0, 2000, 1000, 2000, 2000, 2000, 2000, 2300, 1600, 2400, 2900, 1600, 1600, 2900, 1600, 1000, 4100, 2000, 900],
    [0, 2400, 1200, 2400, 2400, 2400, 2400, 2700, 2000, 3000, 3500, 2000, 2000, 3500, 2000, 1200, 5000, 2500, 1100]
]
yiji_danjia = [
    [0, 8, 6.75, 6.5, 3.75, 6.25, 5.5, 5.75, 5.25, 5.5, 6.5, 5, 5.75, 7, 5.25, 7.25, 4.5, 4.5, 4],
    [0, 8, 6.75, 6.5, 3.75, 6.25, 5.5, 5.75, 5.25, 5.5, 6.5, 5, 5.75, 7, 5.25, 7.25, 4.5, 4.5, 4]
]
erji_muben = [
    #
    [0, 2000, 500, 500],
    [0, 3000, 2000, 10000, 10000],
    [0, 2640, 1320, 2640, 2640, 2640, 2640, 3000, 2200, 3300, 3850, 2200, 2200, 3850, 2200, 1300, 5500, 2750, 1200]
]
erji_danjia = [
    [0, 2.5, 2.5, 3.25],
    [0, 57.5, 19, 16, 100],
    [0, 9.6, 8.1, 7.8, 4.5, 7.5, 6.6, 6.9, 6.8, 6.6, 7.8, 6, 6.9, 8.4, 6.3, 8.7, 5.4, 5.4, 4.8]
]


def doulei(k):
    # 初始化一个空列表来存储符合条件的索引
    indices = []
    # 遍历字符串k中的每个字符及其索引
    for i, idx in enumerate(k):
        if idx == 1:
            indices.append(i)
    for i, idx in enumerate(k):
        if idx == 2:
            k[i] = 1
            indices.append(i)
    for i, idx in enumerate(k):
        if idx == 3:
            k[i] = 2
            indices.append(i)
            # 返回索引列表
    return indices


def doulei2(k):
    # 初始化一个空列表来存储符合条件的索引
    indices = []
    # 遍历字符串k中的每个字符及其索引
    for i, idx in enumerate(k):
        if idx == 1:
            indices.append(i)
    for i, idx in enumerate(k):
        if idx == 2:
            k[i] = 1
            indices.append(i)
    for i, idx in enumerate(k):
        if idx == 3:
            k[i] = 2
            indices.append(i)
    for i, idx in enumerate(k):
        if idx == 4:
            k[i] = 3
            indices.append(i)
    for i, idx in enumerate(k):
        if idx == 5:
            k[i] = 4
            indices.append(i)
    for i, idx in enumerate(k):
        if idx == 6:
            k[i] = 5
            indices.append(i)
            # 返回索引列表
    return indices


def ABCqiujie(k_abc,data):
    profit = 0
    cost = 0
    spaces = [
        0,
        80, 55, 35, 72, 68, 55,
        60, 46, 40, 28, 25, 86, 55, 44, 50, 25, 60, 45, 35, 20,
        15, 13, 15, 18, 27, 20,

        15, 10, 14, 6, 10, 12, 22, 20,
        0.6, 0.6, 0.6, 0.6, 0.6,
        0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6,
        0.6, 0.6, 0.6, 0.6
    ]
    # 作物分季产量约束
    season_food = [
        [0, 57000, 21850, 22400, 33040, 9875, 170840, 132750, 71400,
         30000, 12500, 1500, 35100, 36000, 14000, 10000, 21000,
         36480, 26880, 6480, 30000, 35400, 43200, 0, 1800, 3600, 4050, 4500,
         34400, 9000, 1500, 1200, 3600, 1800, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 810, 2160, 900, 810, 0, 0, 0, 1080, 4050, 1350, 0, 0, 0,
         1800, 150000, 100000, 36000, 9000, 7200, 18000, 4200
         ]]
    c = doulei(k_abc)
    for i, idx in enumerate(c):
        if idx >= 1 and idx <= 6:
            z = 0
            for n in list(range(1, 16)):
                if k1_liangshi[idx] == n:
                    continue
                if season_food[0][n] <= 0 or spaces[idx] == 0:
                    continue
                elif (season_food[0][n] - spaces[idx] * danjichan[z][n]) >= 0:
                    m = spaces[idx]
                    if m != 0:
                        print(files[idx] + '种植' + foods[n] + str(m) + '亩1')
                        data[n][idx] = m
                        profit += m * danjichan[z][n] * danji_danjia[n]
                        cost += m * danji_muben[n]
                        season_food[0][n] -= spaces[idx] * danjichan[z][n]
                        spaces[idx] = 0
                        if n <= 5:
                            k_abc[idx] = 3
                        k1_liangshi[idx] = n
                        break
                else:
                    m = season_food[0][n] / danjichan[z][n]
                    if m < spaces[idx] * 0.050:
                        continue
                    elif (spaces[idx] - m) < spaces[idx] * 0.05:
                        print(files[idx] + '种植' + foods[n] + str(spaces[idx]) + '亩2')
                        data[n][idx] = spaces[idx]
                        profit += spaces[idx] * danjichan[z][n] * danji_danjia[n]
                        cost += spaces[idx] * danji_muben[n]
                        season_food[0][n] -= spaces[idx] * danjichan[z][n]
                        spaces[idx] = 0
                        if n <= 5:
                            k_abc[idx] = 3
                        k1_liangshi[idx] = n
                    else:
                        print(files[idx] + '种植' + foods[n] + str(m) + '亩3')
                        data[n][idx] = m
                        profit += m * danjichan[z][n] * danji_danjia[n]
                        cost += m * danji_muben[n]
                        season_food[0][n] = 0
                        spaces[idx] = spaces[idx] - m
                        if n <= 5:
                            k_abc[idx] = 3
                        k1_liangshi[idx] = n
                        continue
        elif idx >= 7 and idx <= 20:
            z = 1
            for n in list(range(1, 16)):
                if k1_liangshi[idx] == n:
                    continue
                if season_food[0][n] <= 0 or spaces[idx] == 0:
                    continue
                elif (season_food[0][n] - spaces[idx] * danjichan[z][n]) >= 0:
                    m = spaces[idx]
                    if m != 0:
                        print(files[idx] + '种植' + foods[n] + str(m) + '亩1')
                        data[n][idx] = m
                        profit += m * danjichan[z][n] * danji_danjia[n]
                        cost += m * danji_muben[n]
                        season_food[0][n] -= spaces[idx] * danjichan[z][n]
                        spaces[idx] = 0
                        if n <= 5:
                            k_abc[idx] = 3
                        k1_liangshi[idx] = n
                        break
                else:
                    m = season_food[0][n] / danjichan[z][n]
                    if m < spaces[idx] * 0.050:
                        continue
                    elif (spaces[idx] - m) < spaces[idx] * 0.05:
                        print(files[idx] + '种植' + foods[n] + str(spaces[idx]) + '亩2')
                        data[n][idx] = spaces[idx]
                        profit += spaces[idx] * danjichan[z][n] * danji_danjia[n]
                        cost += spaces[idx] * danji_muben[n]
                        season_food[0][n] -= spaces[idx] * danjichan[z][n]
                        spaces[idx] = 0
                        if n <= 5:
                            k_abc[idx] = 3
                        k1_liangshi[idx] = n
                    else:
                        print(files[idx] + '种植' + foods[n] + str(m) + '亩3')
                        data[n][idx] = m
                        profit += m * danjichan[z][n] * danji_danjia[n]
                        cost += m * danji_muben[n]
                        season_food[0][n] = 0
                        spaces[idx] = spaces[idx] - m
                        if n <= 5:
                            k_abc[idx] = 3
                        k1_liangshi[idx] = n
                        continue
        else:
            z = 2
            for n in list(range(1, 16)):
                if k1_liangshi[idx] == n:
                    continue
                if season_food[0][n] <= 0 or spaces[idx] == 0:
                    continue
                elif (season_food[0][n] - spaces[idx] * danjichan[z][n]) >= 0:
                    m = spaces[idx]
                    if m != 0:
                        print(files[idx] + '种植' + foods[n] + str(m) + '亩1')
                        data[n][idx] = m
                        profit += m * danjichan[z][n] * danji_danjia[n]
                        cost += m * danji_muben[n]
                        season_food[0][n] -= m * danjichan[z][n]
                        spaces[idx] = 0
                        if n <= 5:
                            k_abc[idx] = 3
                        k1_liangshi[idx] = n
                        break

                else:
                    m = season_food[0][n] / danjichan[z][n]
                    if m < spaces[idx] * 0.050:
                        continue
                    elif (spaces[idx] - m) < spaces[idx] * 0.05:
                        print(files[idx] + '种植' + foods[n] + str(spaces[idx]) + '亩2')
                        data[n][idx] = spaces[idx]
                        profit += spaces[idx] * danjichan[z][n] * danji_danjia[n]
                        cost += spaces[idx] * danji_muben[n]
                        season_food[0][n] -= spaces[idx] * danjichan[z][n]
                        spaces[idx] = 0
                        if n <= 5:
                            k_abc[idx] = 3
                        k1_liangshi[idx] = n
                    else:
                        print(files[idx] + '种植' + foods[n] + str(m) + '亩3')
                        data[n][idx] = m
                        profit += m * danjichan[z][n] * danji_danjia[n]
                        cost += m * danji_muben[n]
                        season_food[0][n] = 0
                        spaces[idx] = spaces[idx] - m
                        if n <= 5:
                            k_abc[idx] = 3
                        k1_liangshi[idx] = n
                        continue
    print(spaces)
    print(season_food[0])
    return profit, cost


def DEFqiujie_1(k_def,data):
    profit = 0
    cost = 0
    spaces1 = [
        0,
        80, 55, 35, 72, 68, 55,
        60, 46, 40, 28, 25, 86, 55, 44, 50, 25, 60, 45, 35, 20,
        15, 13, 15, 18, 27, 20,

        15, 10, 14, 6, 10, 12, 22, 20,
        0.6, 0.6, 0.6, 0.6, 0.6,
        0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6,
        0.6, 0.6, 0.6, 0.6
    ]
    spaces = [
        0,
        80, 55, 35, 72, 68, 55,
        60, 46, 40, 28, 25, 86, 55, 44, 50, 25, 60, 45, 35, 20,
        15, 13, 15, 18, 27, 20,

        15, 10, 14, 6, 10, 12, 22, 20,
        0.6, 0.6, 0.6, 0.6, 0.6,
        0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6,
        0.6, 0.6, 0.6, 0.6
    ]
    # 作物分季产量约束
    season_food = [
        [0, 57000, 21850, 22400, 33040, 9875, 170840, 132750, 71400,
         30000, 12500, 1500, 35100, 36000, 14000, 10000, 21000,
         36480, 26880, 6480, 30000, 35400, 43200, 0, 1800, 3600, 4050, 4500,
         34400, 9000, 1500, 1200, 3600, 1800, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 810, 2160, 900, 810, 0, 0, 0, 1080, 4050, 1350, 0, 0, 0,
         1800, 150000, 100000, 36000, 9000, 7200, 18000, 4200
         ]]
    c = doulei2(k_def)
    for i, idx in enumerate(c):
        if idx >= 1 and idx <= 8:
            z = 0
            if k1_shucai_1[idx] != 16 and spaces[idx + 26] != 0:
                if season_food[0][16] > 0:
                    if season_food[0][16] - spaces[idx + 26] * danjichan[z][16] > 0:
                        m = spaces[idx + 26]
                        if m != 0:
                            print(files[idx + 26] + '种植' + foods[16] + str(m) + '亩1')
                            data[16][idx+26] = m
                            profit += m * 7 * 500
                            cost += m * 680
                            season_food[0][16] -= spaces[idx + 26] * danjichan[z][16]
                            spaces[idx + 26] = 0
                            k1_shucai_1[idx] = 16

            for n in list(range(17, 35)):
                if k1_shucai_1[idx] == n:
                    continue
                if season_food[0][n] <= 0 or spaces[idx + 26] == 0:
                    continue
                elif (season_food[0][n] - spaces[idx + 26] * danjichan[z][n]) >= 0:
                    m = spaces[idx + 26]
                    if m != 0:
                        print(files[idx + 26] + '种植' + foods[n] + str(m) + '亩1')
                        data[n][idx+26] = m
                        profit += m * danjichan[z][n] * yiji_danjia[0][n - 16]
                        cost += m * yiji_muben[0][n - 16]
                        season_food[0][n] -= spaces[idx + 26] * danjichan[z][n]
                        spaces[idx + 26] = 0
                        if n <= 19:
                            k_def[idx] = 6
                        k1_shucai_1[idx] = n
                        break
                else:
                    m = season_food[0][n] / danjichan[z][n]
                    if m < spaces1[idx + 26] * 0.050:
                        continue
                    elif (spaces[idx + 26] - m) < spaces1[idx + 26] * 0.05:
                        print(files[idx + 26] + '种植' + foods[n] + str(spaces[idx + 26]) + '亩2')
                        data[n][idx+26] = spaces[idx+26]
                        profit += spaces[idx + 26] * danjichan[z][n] * yiji_danjia[0][n - 16]
                        cost += spaces[idx + 26] * yiji_muben[0][n - 16]
                        season_food[0][n] -= spaces[idx + 26] * danjichan[z][n]
                        spaces[idx + 26] = 0
                        if n <= 19:
                            k_def[idx] = 6
                        k1_shucai_1[idx] = n
                    else:
                        print(files[idx + 26] + '种植' + foods[n] + str(m) + '亩3')
                        data[n][idx+26] = m
                        profit += m * danjichan[z][n] * yiji_danjia[0][n - 16]
                        cost += m * yiji_muben[0][n - 16]
                        season_food[0][n] = 0
                        spaces[idx + 26] = spaces[idx + 26] - m
                        if n <= 19:
                            k_def[idx] = 6
                        k1_shucai_1[idx] = n
                        continue

        if idx >= 9 and idx <= 24:
            z = 1
            for n in list(range(17, 35)):
                if k1_shucai_1[idx] == n:
                    continue
                if season_food[0][n] <= 0 or spaces[idx + 26] == 0:
                    continue
                elif (season_food[0][n] - spaces[idx + 26] * danjichan[z][n]) >= 0:
                    m = spaces[idx + 26]
                    if m != 0:
                        print(files[idx + 26] + '种植' + foods[n] + str(m) + '亩1')
                        data[n][idx+26] = m
                        profit += m * danjichan[z][n] * yiji_danjia[1][n - 16]
                        cost += m * yiji_muben[1][n - 16]
                        season_food[0][n] -= spaces[idx + 26] * danjichan[z][n]
                        spaces[idx + 26] = 0
                        if n <= 19:
                            k_def[idx] = 6
                        k1_shucai_1[idx] = n
                        break
                else:
                    m = season_food[0][n] / danjichan[z][n]
                    if m < spaces1[idx + 26] * 0.050:
                        continue
                    elif (spaces[idx + 26] - m) < spaces1[idx + 26] * 0.05:
                        print(files[idx + 26] + '种植' + foods[n] + str(spaces[idx + 26]) + '亩2')
                        data[n][idx+26] = spaces[idx+26]
                        profit += spaces[idx + 26] * danjichan[z][n] * yiji_danjia[1][n - 16]
                        cost += spaces[idx + 26] * yiji_muben[1][n - 16]
                        season_food[0][n] -= spaces[idx + 26] * danjichan[z][n]
                        spaces[idx + 26] = 0
                        if n <= 19:
                            k_def[idx] = 6
                        k1_shucai_1[idx] = n
                    else:
                        print(files[idx + 26] + '种植' + foods[n] + str(m) + '亩3')
                        data[n][idx+26] = m
                        profit += m * danjichan[z][n] * yiji_danjia[1][n - 16]
                        cost += m * yiji_muben[1][n - 16]
                        season_food[0][n] = 0
                        spaces[idx + 26] = spaces[idx + 26] - m
                        if n <= 19:
                            k_def[idx] = 6
                        k1_shucai_1[idx] = n
                        continue
        if idx >= 25 and idx <= 28:
            z = 1
            for n in list(range(17, 35)):
                if k1_shucai_1[idx] == n:
                    continue
                if season_food[0][n] <= 0 or spaces[idx + 26] == 0:
                    continue
                elif (season_food[0][n] - spaces[idx + 26] * danjichan[z][n]) >= 0:
                    m = spaces[idx + 26]
                    if m != 0:
                        print(files[idx + 26] + '种植' + foods[n] + str(m) + '亩1')
                        data[n][idx+26] = m
                        profit += m * danjichan[z][n] * yiji_danjia[1][n - 16]
                        cost += m * yiji_muben[1][n - 16]
                        season_food[0][n] -= spaces[idx + 26] * danjichan[z][n]
                        spaces[idx + 26] = 0
                        if n <= 19:
                            k_def[idx] = 6
                        k1_shucai_1[idx] = n
                        break
                else:
                    m = season_food[0][n] / danjichan[z][n]
                    if m < spaces1[idx + 26] * 0.050:
                        continue
                    elif (spaces[idx + 26] - m) < spaces1[idx + 26] * 0.05:
                        print(files[idx + 26] + '种植' + foods[n] + str(spaces[idx + 26]) + '亩2')
                        data[n][idx+26] = spaces[idx+26]
                        profit += spaces[idx + 26] * danjichan[z][n] * yiji_danjia[1][n - 16]
                        cost += spaces[idx + 26] * yiji_muben[1][n - 16]
                        season_food[0][n] -= spaces[idx + 26] * danjichan[z][n]
                        spaces[idx + 26] = 0
                        if n <= 19:
                            k_def[idx] = 6
                        k1_shucai_1[idx] = n
                    else:
                        print(files[idx + 26] + '种植' + foods[n] + str(m) + '亩3')
                        data[n][idx+26] = m
                        profit += m * danjichan[z][n] * yiji_danjia[1][n - 16]
                        cost += m * yiji_muben[1][n - 16]
                        season_food[0][n] = 0
                        spaces[idx + 26] = spaces[idx + 26] - m
                        if n <= 19:
                            k_def[idx] = 6
                        k1_shucai_1[idx] = n
                        continue
    print(spaces)
    print(season_food[0])
    return profit, cost


def DEFqiujie_2(k_def,data):
    profit = 0
    cost = 0
    spaces1 = [
        0,
        80, 55, 35, 72, 68, 55,
        60, 46, 40, 28, 25, 86, 55, 44, 50, 25, 60, 45, 35, 20,
        15, 13, 15, 18, 27, 20,

        15, 10, 14, 6, 10, 12, 22, 20,
        0.6, 0.6, 0.6, 0.6, 0.6,
        0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6,
        0.6, 0.6, 0.6, 0.6
    ]
    spaces = [
        0,
        80, 55, 35, 72, 68, 55,
        60, 46, 40, 28, 25, 86, 55, 44, 50, 25, 60, 45, 35, 20,
        15, 13, 15, 18, 27, 20,

        15, 10, 14, 6, 10, 12, 22, 20,
        0.6, 0.6, 0.6, 0.6, 0.6,
        0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6,
        0.6, 0.6, 0.6, 0.6
    ]
    # 作物分季产量约束
    season_food = [
        [0, 57000, 21850, 22400, 33040, 9875, 170840, 132750, 71400,
         30000, 12500, 1500, 35100, 36000, 14000, 10000, 21000,
         36480, 26880, 6480, 30000, 35400, 43200, 0, 1800, 3600, 4050, 4500,
         34400, 9000, 1500, 1200, 3600, 1800, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 810, 2160, 900, 810, 0, 0, 0, 1080, 4050, 1350, 0, 0, 0,
         1800, 150000, 100000, 36000, 9000, 7200, 18000, 4200
         ]]
    c = doulei2(k_def)
    for i, idx in enumerate(c):
        if idx >= 1 and idx <= 8:
            if k1_shucai_1[idx] != 16 and spaces[idx + 26] != 0:
                for n in [35, 36, 37]:
                    if season_food[1][n] <= 0:
                        continue
                    elif (season_food[1][n] - spaces[idx + 26] * twojichan[n - 16]) >= 0:
                        m = spaces[idx + 26]
                        if m != 0:
                            print(files[idx + 26] + '种植' + foods[n] + str(m) + '亩1')
                            data[n][idx+54] = m
                            profit += m * twojichan[n - 16] * erji_danjia[0][n - 34]
                            cost += m * erji_muben[0][n - 34]
                            season_food[1][n] -= spaces[idx + 26] * twojichan[n - 16]
                            spaces[idx + 26] = 0
                            k1_shucai_1[idx] = n
                            break
                    else:
                        m = season_food[1][n] / twojichan[n - 16]
                        if m < spaces1[idx + 26] * 0.050:
                            continue
                        elif (spaces[idx + 26] - m) < spaces1[idx + 26] * 0.05:
                            print(files[idx + 26] + '种植' + foods[n] + str(spaces[idx + 26]) + '亩2')
                            data[n][idx + 54] = spaces[idx + 26]
                            profit += spaces[idx + 26] * twojichan[n - 16] * erji_danjia[0][n - 34]
                            cost += spaces[idx + 26] * erji_muben[0][n - 34]
                            season_food[1][n] -= spaces[idx + 26] * twojichan[n - 16]
                            spaces[idx + 26] = 0
                            k1_shucai_1[idx] = n
                        else:
                            print(files[idx + 26] + '种植' + foods[n] + str(m) + '亩3')
                            data[n][idx+54] = m
                            profit += m * twojichan[n - 16] * erji_danjia[0][n - 34]
                            cost += m * erji_muben[0][n - 34]
                            season_food[1][n] = 0
                            spaces[idx + 26] = spaces[idx + 26] - m
                            k1_shucai_1[idx] = n
                            continue
        if idx >= 9 and idx <= 24:
            if k1_shucai_1[idx] != 16 and spaces[idx + 26] != 0:
                for n in [38, 39, 40, 41]:
                    if season_food[1][n] <= 0:
                        continue
                    elif (season_food[1][n] - spaces[idx + 26] * twojichan[n - 16]) >= 0:
                        m = spaces[idx + 26]
                        if m != 0:
                            print(files[idx + 26] + '种植' + foods[n] + str(m) + '亩1')
                            data[n][idx+54] = m
                            profit += m * twojichan[n - 16] * erji_danjia[1][n - 37]
                            cost += m * erji_muben[1][n - 37]
                            season_food[1][n] -= spaces[idx + 26] * twojichan[n - 16]
                            spaces[idx + 26] = 0
                            k1_shucai_1[idx] = n
                            break
                    else:
                        m = season_food[1][n] / twojichan[n - 16]
                        if m < spaces1[idx + 26] * 0.050:
                            continue
                        elif (spaces[idx + 26] - m) < spaces1[idx + 26] * 0.05:
                            print(files[idx + 26] + '种植' + foods[n] + str(spaces[idx + 26]) + '亩2')
                            data[n][idx + 54] = spaces[idx + 26]
                            profit += spaces[idx + 26] * twojichan[n - 16] * erji_danjia[1][n - 37]
                            cost += spaces[idx + 26] * erji_muben[1][n - 37]
                            season_food[1][n] -= spaces[idx + 26] * twojichan[n - 16]
                            spaces[idx + 26] = 0
                            k1_shucai_1[idx] = n
                        else:
                            print(files[idx + 26] + '种植' + foods[n] + str(m) + '亩3')
                            data[n][idx+54] = m
                            profit += m * twojichan[n - 16] * erji_danjia[1][n - 37]
                            cost += m * erji_muben[1][n - 37]
                            season_food[1][n] = 0
                            spaces[idx + 26] = spaces[idx + 26] - m
                            k1_shucai_1[idx] = n
                            continue
        if idx >= 25 and idx <= 28:
            for n in list(range(17, 35)):
                if k1_shucai_1[idx] == n:
                    continue
                if season_food[1][n] <= 0 or spaces[idx + 26] == 0:
                    continue
                elif (season_food[1][n] - spaces[idx + 26] * twojichan[n - 16]) >= 0:
                    m = spaces[idx + 26]
                    if m != 0:
                        print(files[idx + 26] + '种植' + foods[n] + str(m) + '亩1')
                        data[n][idx + 54] = m
                        profit += m * twojichan[n - 16] * erji_danjia[2][n - 16]
                        cost += m * erji_muben[2][n - 16]
                        season_food[1][n] -= spaces[idx + 26] * twojichan[n - 16]
                        spaces[idx + 26] = 0
                        if n <= 19:
                            k_def[idx] = 6
                        k1_shucai_1[idx] = n
                        break
                else:
                    m = season_food[1][n] / twojichan[n - 16]
                    if m < spaces1[idx + 26] * 0.050:
                        continue
                    elif (spaces[idx + 26] - m) < spaces1[idx + 26] * 0.05:
                        print(files[idx + 26] + '种植' + foods[n] + str(spaces[idx + 26]) + '亩2')
                        data[n][idx + 54] = spaces[idx+26]
                        profit += spaces[idx + 26] * twojichan[n - 16] * erji_danjia[2][n - 16]
                        cost += spaces[idx + 26] * erji_muben[2][n - 16]
                        season_food[1][n] -= spaces[idx + 26] * twojichan[n - 16]
                        spaces[idx + 26] = 0
                        if n <= 19:
                            k_def[idx] = 6
                        k1_shucai_1[idx] = n
                    else:
                        print(files[idx + 26] + '种植' + foods[n] + str(m) + '亩3')
                        data[n][idx + 54] = m
                        profit += m * twojichan[n - 16] * erji_danjia[2][n - 16]
                        cost += m * erji_muben[2][n - 16]
                        season_food[1][n] = 0
                        spaces[idx + 26] = spaces[idx + 26] - m
                        if n <= 19:
                            k_def[idx] = 6
                        k1_shucai_1[idx] = n
                        continue
    print(spaces)
    print(season_food[1])
    return profit, cost


def main1(sheet_name):
    data = pd.DataFrame(0, index=range(1, 83), columns=range(1, 42))
    profit = 0
    cost = 0
    print("\n\n")
    print("单季")
    profit, cost = ABCqiujie(k_abc,data)
    print(profit, cost)
    print("\n\n")
    print("第一季")
    profit, cost = DEFqiujie_1(k_def,data)
    print(profit, cost)
    print("\n\n")
    print("第二季")
    profit, cost = DEFqiujie_2(k_def,data)
    print(profit, cost)

    # excel_filename = 'result1_1.xlsx'
    #
    # # 使用ExcelWriter和openpyxl引擎
    # with pd.ExcelWriter(excel_filename, engine='openpyxl', mode='a', if_sheet_exists='replace') as writer:
    #     sheet_name = sheet_name
    #     data.to_excel(writer, sheet_name=sheet_name, startrow=1, startcol=2, header=False, index=False)
    # 创建x和y坐标网格
    x = np.linspace(0, 41, 41)
    y = np.linspace(0, 82, 82)
    X, Y = np.meshgrid(x, y)

    # 这里Z就是你的data数组，它已经是三维曲面图所需的高度数据
    Z = data

    # 创建一个图形和三维坐标轴
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')

    # 绘制三维曲面图
    surf = ax.plot_surface(X, Y, Z, cmap='viridis', edgecolor='none')

    # 设置坐标轴标签
    ax.set_xlabel('X Label')
    ax.set_ylabel('Y Label')
    ax.set_zlabel('Z Label')

    # 显示图形
    plt.show()





print("2024年", end='\n\n\n')
main1("2024")
print("2025年", end='\n\n\n')
main1('2025')
print("2026年", end='\n\n\n')
main1('2026')
print("2027年", end='\n\n\n')
main1('2027')
print("2028年", end='\n\n\n')
main1('2028')
print("2029年", end='\n\n\n')
main1('2029')
print("2030年", end='\n\n\n')
main1('2030')
